<template>
	<view class="container">
		<uni-search-bar cancelText="确定" @cancel="searchItem" v-model="searchText" @confirm="searchItem"
			bgColor="#f8f8f8" :radius="30" placeholder="请输入要搜索的名称"></uni-search-bar>
		<u-index-list :index-list="indexList">
			<template v-for="(item, index) in itemArr">
				<!-- #ifdef APP-NVUE -->
				<u-index-anchor :text="indexList[index]"></u-index-anchor>
				<!-- #endif -->
				<u-index-item>
					<!-- #ifndef APP-NVUE -->
					<u-index-anchor :text="indexList[index]"></u-index-anchor>
					<!-- #endif -->
					<!-- 个人 -->
					<view v-if="version == 1">
						<view class="list-cell " @click="$store.commit('toUserIndex',cell)"
							v-for="(cell, index) in item">
							<image style="width: 65rpx;height: 65rpx;border-radius: 50%;"
								@click="$store.commit('toUserIndex',cell)"
								:src="'https://www.ydylmold.cn/images/ydylapp/usrCraftsman/image/'+cell.headImg"
								mode=""></image>
							<text class="u-m-x-15"> {{cell.usrName}}</text>
						</view>
					</view>
					<!-- 企业 -->
					<view v-if="version == 2">
						<view class="list-cell " v-for="(cell, index) in item"
							@click="$store.commit('toUserIndex',cell)">
							<image style="width: 65rpx;height: 65rpx;border-radius: 50%;"
								:src="'https://www.ydylmold.cn/images/ydylapp/usrCompany/image/'+cell.headImg" mode="">
							</image>
							<text class="u-m-x-15"> {{cell.usrName}}</text>
						</view>
					</view>
				</u-index-item>
			</template>
		</u-index-list>
		<my-empty tipText="数据为空" image="http://cdn.uviewui.com/uview/empty/data.png" v-if="isEmpty"></my-empty>
		<u-loadmore :status="status" v-else></u-loadmore>
	</view>
</template>
<script>
	import {
		pySegSort,
		duplicateRemoval
	} from '@/utils/common.js'
	import {
		getToken
	} from '@/utils/token.js'
	import {
		usrBlackListQuery
	} from '@/common/api/msg.js'
	export default {
		data() {
			return {
				indexList: [],
				itemArr: [],
				acList: [],
				allpage: 1,
				currPage: 1,
				isEmpty: false,
				status: 'loading',
				searchText: '',
				version: getToken('version')
			}
		},
		onLoad: function() {
			this.usrBlackListQuery()
		},
		onReachBottom: function() {
			if (this.status == 'loadmore') {
				this.currPage++
				this.usrBlackListQuery()
			}
		},
		// 触发下拉刷新
		onPullDownRefresh() {
			this.currPage = 1
			this.acList = []
			this.usrBlackListQuery()
			// 延迟关闭刷新动画
			setTimeout(() => {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		methods: {
			toUrl(path) {
				uni.navigateTo({
					url: path
				})
			},
			searchItem() {
				this.currPage = 1
				this.acList = []
				this.usrBlackListQuery()
			},
			usrBlackListQuery() {
				this.status = 'loading'
				let params = {
					userId: getToken('useNum'),
					userType: getToken('version') == 1 ? 0 : 1,
					blacklist: 1,
					'page.current': this.currPage,
					searchText: this.searchText
				};
				usrBlackListQuery(params).then(res => {
					if (res.data) {
						let list = res.data && res.data && res.data.rows
						if (list && list.length) {
							list.forEach(m => {
								if(m.moduleNo == 'ydylapp1002' || m.moduleNo == 'ydylapp1003'|| m.moduleNo == 'ydylapp1001'){
									m.companyId = m.userId
								}
							})
						}
						this.acList = duplicateRemoval(this.acList, list || [],
							'craftsmanId')
						this.allpage = res.data && res.data.total
						this.currPage < this.allpage ? this.status = 'loadmore' : this.status = 'nomore'
						this.isEmpty = this.acList.length == 0 ? true : false
						this.acList = this.acList.filter(m => {
							return m.usrName
						})
						let arr = pySegSort(this.acList, 'usrName')
						this.indexList = arr.map(v => {
							return v.letter
						})
						this.itemArr = arr.map(v => {
							return v.data
						})
					}
				});
			}
		}
	}
</script>
<style lang="scss" scoped>
	.list-cell {
		display: flex;
		box-sizing: border-box;
		width: 100%;
		padding: 10px 24rpx;
		overflow: hidden;
		color: #323233;
		font-size: 14px;
		line-height: 24px;
		background-color: #fff;
	}
</style>
